package com.spa.application.service.admin;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.spa.application.command.admin.CityAddCommand;
import com.spa.application.command.admin.CityConfigTravelCommand;
import com.spa.application.command.admin.CityUpdateCommand;
import com.spa.application.dto.admin.CityDTO;
import com.spa.application.dto.admin.CityExportDTO;
import com.spa.domain.entity.CitiesServed;
import com.spa.domain.service.CitiesServedService;
import com.spa.infrastructure.exception.SpaException;
import com.spa.infrastructure.mapper.CitiesServedMapperStruct;
import com.spa.infrastructure.repository.condition.CitiesServedCondition;
import com.spa.infrastructure.service.gaode.GaodeService;
import com.spa.infrastructure.service.gaode.result.ReGeoResult;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CityApplicationService {

    @Autowired
    private CitiesServedService citiesServedService;

    @Autowired
    private GaodeService gaodeService;

    public List<CityDTO> findAllCities(Integer status) {
        CitiesServedCondition condition = new CitiesServedCondition();
        condition.setStatus(status);
        return CitiesServedMapperStruct.INSTANCE.entity2CityDto4List(citiesServedService.listInfo(condition));
    }

    public List<CityExportDTO> findAllCitiesExport(Integer status) {
        CitiesServedCondition condition = new CitiesServedCondition();
        condition.setStatus(status);
        return CitiesServedMapperStruct.INSTANCE.entity2CityExportDto4List(citiesServedService.listInfo(condition));
    }

    public CityDTO getCityByLocation(String location) {
        log.info("getCityByLocation : {}", location);
        List<CityDTO> cityDTOS = findAllCities(10);
        try {
            ReGeoResult.AddressComponent addressComponent = gaodeService.regeo(location);
            log.info("{}", addressComponent);
            String cityName;
            if (StringUtils.isNotEmpty(addressComponent.getCity())) {
                cityName = addressComponent.getCity();
            } else {
                cityName = addressComponent.getProvince();
            }
            List<CityDTO> cityDTOS1 = cityDTOS.stream().filter(cityDTO -> cityDTO.getCityName().equals(cityName)).toList();
            if (CollectionUtil.isNotEmpty(cityDTOS1)) {
                return cityDTOS1.get(0);
            }
        } catch (Exception e) {
            log.error("getCityByLocation", e);
        }
        return cityDTOS.stream().filter(cityDTO -> cityDTO.getCityName().equals("太原市")).toList().get(0);
    }

    public void addCity(CityAddCommand command) {
        citiesServedService.add(command);
    }

    public void close(CityUpdateCommand command) {
        citiesServedService.close(command.getId());
    }

    public void open(CityUpdateCommand command) {
        citiesServedService.open(command.getId());
    }

    public void configTravel(CityConfigTravelCommand command) {
        CitiesServed citiesServed = citiesServedService.getById(command.getId());
        if (ObjectUtil.isNull(citiesServed)) {
            throw new SpaException("请先开通城市！");
        }
        citiesServedService.travelConfig(command);
    }

    public CityDTO getById(long id) {
        CitiesServed citiesServed = citiesServedService.getById(id);
        return CitiesServedMapperStruct.INSTANCE.entity2CityDto(citiesServed);
    }

}
